﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using WebCrawler.Service;

namespace WebCrawler.ViewModel
{
    public class CubemapToPanoramaViewModel : ViewModelBase
    {
        public CubemapToPanoramaViewModel()
        {
            Url = AppDomain.CurrentDomain.BaseDirectory;
        }

        #region 属性
        public string Url
        {
            get { return _url; }
            set { _url = value; RaisePropertyChanged(); }
        }
        private string _url;

        public string StepMessage
        {
            get { return _stepMessage; }
            set { _stepMessage = value; RaisePropertyChanged(); }
        }
        private string _stepMessage;

        public bool IsConverting
        {
            get { return _isConverting; }
            set { _isConverting = value; RaisePropertyChanged(); }
        }
        private bool _isConverting = false;
        #endregion

        #region 命令
        public RelayCommand StartConvertCmd
        {
            get
            {
                if (_startConvertCmd == null)
                    _startConvertCmd = new RelayCommand(startConvert);
                return _startConvertCmd;
            }
            set { _startConvertCmd = value; }
        }
        public RelayCommand _startConvertCmd;
        #endregion

        #region 辅助函数
        private async void startConvert()
        {
            IsConverting = true;

            //Dictionary<string, Bitmap> bitmaps = new Dictionary<string, Bitmap>();
            //Bitmap bitmap = new Bitmap($"{Url}/f.jpg");
            //bitmaps.Add("f", bitmap);

            //bitmap = new Bitmap($"{Url}/b.jpg");
            //bitmaps.Add("b", bitmap);

            //bitmap = new Bitmap($"{Url}/l.jpg");
            //bitmaps.Add("l", bitmap);

            //bitmap = new Bitmap($"{Url}/r.jpg");
            //bitmaps.Add("r", bitmap);

            //bitmap = new Bitmap($"{Url}/u.jpg");
            //bitmaps.Add("u", bitmap);

            //bitmap = new Bitmap($"{Url}/d.jpg");
            //bitmaps.Add("d", bitmap);

            //StepMessage = $"合并：Cubemap";
            //Bitmap cubemap = await ImageUtility.MergeCubemapAsync(Url, bitmaps);

            //StepMessage = $"合并：Panorama";
            //await ImageUtility.CubemapToPanoramaAsync(Url, cubemap);

            Dictionary<string, Mat> mats = new Dictionary<string, Mat>();
            Mat mat = new Mat($"{Url}/f.jpg", ImreadModes.Color);
            mats.Add("f", mat);

            mat = new Mat($"{Url}/b.jpg", ImreadModes.Color);
            mats.Add("b", mat);

            mat = new Mat($"{Url}/l.jpg", ImreadModes.Color);
            mats.Add("l", mat);

            mat = new Mat($"{Url}/r.jpg", ImreadModes.Color);
            mats.Add("r", mat);

            mat = new Mat($"{Url}/u.jpg", ImreadModes.Color);
            mats.Add("u", mat);

            mat = new Mat($"{Url}/d.jpg", ImreadModes.Color);
            mats.Add("d", mat);

            StepMessage = $"合并：Cubemap";
            Mat cubemap = await ImageUtility.MergeCubemapAsync(Url, mats);

            StepMessage = $"合并：Panorama";
            await ImageUtility.CubemapToPanoramaAsync(Url, cubemap);

            IsConverting = false;
            StepMessage = "完成";
        }
        #endregion

        public void ReleaseRegister()
        {
            Messenger.Default.Unregister(this);
        }
    }
}